package com.lbb.waimai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbb.waimai.entiry.*;
import com.lbb.waimai.entiry.vo.FoodVo;
import com.lbb.waimai.entiry.vo.PageVo;
import com.lbb.waimai.result.R;
import com.lbb.waimai.service.*;
import com.lbb.waimai.utils.JwtHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private FoodService foodService;

    @Autowired
    private UserService userService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AdminService adminService;



    //获取管理员信息
    @GetMapping("getAdmin")
    public R getAdmin(HttpServletRequest request){
        Long userId = JwtHelper.getUserId(request);
        int id = Integer.parseInt(userId.toString());
        Admin admin = adminService.getById(id);
        return R.ok().data("data",admin);
    }

    //修改管理员密码
    @PostMapping("resetPwd")
    public R resetPwd(@RequestBody Admin admin){
        adminService.resetPwd(admin);
        return R.ok();
    }
    //管理员登录
    @PostMapping("login")
    public R login(@RequestBody Admin admin){
       String adToken = adminService.login(admin);
       return R.ok().data("token",adToken);
    }

    //删除分类
    @DeleteMapping("deleteCategory")
    public R deleteCategory(@RequestParam Integer id) {
        categoryService.removeById(id);

        return R.ok();
    }

    //修改分类
    @PostMapping("updateCategory")
    public R updateCategory(@RequestBody Category category) {
        categoryService.update(category);
        return R.ok();
    }

    //添加分类
    @PostMapping("saveCategory")
    public R saveCategory(@RequestBody Category category) {
        System.out.println(category);
        categoryService.save(category);
        return R.ok();
    }

    //查询所有菜品
    @GetMapping("findAllFood")
    public R findAllFood() {
        List<FoodVo> foodVoList = foodService.findAllFood();
        return R.ok().data("data", foodVoList);
    }


    //添加菜品
    @PostMapping("saveFood")
    public R saveFood(@RequestBody FoodVo foodVo) {
        if (foodVo != null){
            Food food = new Food();
            BeanUtils.copyProperties(foodVo,food);
            foodService.save(food);
        }
        return R.ok();
    }

    //删除菜品
    @DeleteMapping("deleteFood")
    public R saveFood(@RequestParam Integer id) {
        foodService.removeById(id);
        return R.ok();
    }

    //修改菜品
    @PostMapping("updateFood")
    public R updateFood(@RequestBody FoodVo foodVo) {
       if (foodVo!= null){
           Food food = new Food();
           BeanUtils.copyProperties(foodVo,food);
           foodService.updateById(food);
       }
        return R.ok();
    }

    //管理用户
    @GetMapping("findAllUser")
    public R findAllUser() {
        List<User> users = userService.list();
        return R.ok().data("data", users);
    }

    //锁定或解锁用户
    @GetMapping("lockOrUnlockUser")
    public R lockOrUnlockUser(Integer id) {
        userService.lockOrUnlockUser(id);
        return R.ok();
    }

    //获取商家信息
    @GetMapping("findSeller")
    public R findSeller(Integer id) {
        Seller seller = sellerService.getById(id);
        return R.ok().data("data", seller);
    }

    //修改商家信息
    @PostMapping("updateSeller")
    public R updateSeller(@RequestBody Seller seller) {
        if (seller != null) {
            sellerService.updateById(seller);
        }
        return R.ok();
    }

    //查看订单
    @PostMapping("findAllOrder")
    public R findAllOrder(@RequestBody PageVo pageVo) {
        Page<Order> orders = orderService.findAllOrder(pageVo);
        return R.ok().data("data", orders);
    }

    //处理退款请求
    @GetMapping("refund")
    public R refund(@RequestParam Integer id,@RequestParam Integer status) {
        Order order = orderService.getById(id);
        //0未支付，1已支付，3为已完成，4为退款中,5为已退款
        //设置为1是拒绝退款，设置为5是同意退款
        if (status == 5){
            order.setStatus(5);
        }
        if (status == 1){
            order.setStatus(1);
        }
        orderService.updateById(order);
        return R.ok();
    }

    //获取所有的用户信息
    @GetMapping("getAllUser")
    public R getAllUser(){
        List<User> users = userService.list();
        return R.ok().data("data",users);
    }
    //认证或撤销用户
    @GetMapping("lockOrUnlock")
    public R lockOrUnlock(@RequestParam Integer id,@RequestParam Integer status){
        User user = userService.getById(id);
        //0为撤销，1为认证
        user.setStatus(status);
        userService.updateById(user);
        return R.ok();
    }

}
